//: C25:Observable.h
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// The Observable class
#ifndef OBSERVABLE_H
#define OBSERVABLE_H
#include "Observer.h"
#include <set>

class Observable
{
    bool changed;
    std::set<Observer*> observers;
protected:
    virtual void setChanged() {
        changed = true;
    }
    virtual void clearChanged() {
        changed = false;
    }
public:
    virtual void addObserver(Observer& o) {
        observers.insert(&o);
    }
    virtual void deleteObserver(Observer& o) {
        observers.erase(&o);
    }
    virtual void deleteObservers() {
        observers.clear();
    }
    virtual int countObservers() {
        return observers.size();
    }
    virtual bool hasChanged() {
        return changed;
    }
    // If this object has changed, notify all
    // of its observers:
    virtual void notifyObservers(Argument* arg=0) {
        if (!hasChanged()) return;
        clearChanged(); // Not "changed" anymore
        std::set<Observer*>::iterator it;
        for (it = observers.begin();
                it != observers.end(); it++)
            (*it)->update(this, arg);
    }
};
#endif // OBSERVABLE_H ///:~
